package com.ds.topmenu.plugins.bpm;

import com.ds.bpm.plugins.nav.ListenerTypeTree;
import com.ds.bpm.plugins.nav.PluginTypeTree;
import com.ds.common.util.StringUtility;
import com.ds.config.TreeListResultModel;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavTreeViewAnnotation;

import com.ds.web.annotation.Aggregation;
import com.ds.web.annotation.AggregationType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping(value = {"/action/bpm/config/"})
@Aggregation(type=AggregationType.menu)
public class BPMConfigAction {


    @RequestMapping(method = RequestMethod.POST, value = "PluginNavTree")
    @ModuleAnnotation(dynLoad = true, caption = "工作流插件", imageClass = "bpmfont bpmgongzuoliuxitongpeizhi")
    @CustomAnnotation(index = 0)
    @NavTreeViewAnnotation
    @ResponseBody
    public TreeListResultModel<List<PluginTypeTree>> getPlugins(String id, String projectName) {
        TreeListResultModel<List<PluginTypeTree>> result = new TreeListResultModel<>();
        List<PluginTypeTree> plugins = new ArrayList<>();
        PluginTypeTree tree = new PluginTypeTree("admin.bpd.Plugins",projectName);
        plugins.add(tree);
        if (id != null && !id.equals("")) {
            String[] orgIdArr = StringUtility.split(id, ";");
            result.setIds(Arrays.asList(orgIdArr));
        } else {
            result.setIds(Arrays.asList(new String[]{tree.getFristClassItem(tree).getId()}));
        }

        result.setData(plugins);
        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "ListenerNavTree")
    @NavTreeViewAnnotation
    @CustomAnnotation(index = 1)
    @ModuleAnnotation(dynLoad = true, imageClass = "spafont spa-icon-conf1", caption = "监听器")
    @ResponseBody
    public TreeListResultModel<List<ListenerTypeTree>> getListeners(String id, String projectName) {
        TreeListResultModel<List<ListenerTypeTree>> result = new TreeListResultModel<>();
        List<ListenerTypeTree> plugins = new ArrayList<>();
        ListenerTypeTree tree = new ListenerTypeTree("admin.bpd.listener.Listeners",projectName);
        plugins.add(tree);
        if (id != null && !id.equals("")) {
            String[] orgIdArr = StringUtility.split(id, ";");
            result.setIds(Arrays.asList(orgIdArr));
        } else {
            result.setIds(Arrays.asList(new String[]{tree.getFristClassItem(tree).getId()}));
        }

        result.setData(plugins);

        return result;
    }


}
